package com.java.imauoj.controler;

import com.java.imauoj.Tools.Function;
import com.java.imauoj.moudel.*;
import com.java.imauoj.sercive.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/v1")
public class ProblemControler {

    @Autowired
    private ProblemService service;

    @PostMapping("getProblemList")
    public ResultVo getProblemList(@RequestBody Filter filter,HttpSession session){

        ResultVo<List<Problem>> re = new ResultVo();
        User userLogin = (User)session.getAttribute("login");
        if(userLogin!=null){
            Role role = (Role)session.getAttribute("role");
            if(role.getIs_get_problem() == 0){
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else{
                re.setStatusCode(1);
                re.setMessage("成功");
                Function function = new Function();
                FanHuiZhi fhz = function.qinjie(filter);
                List<Problem> list = service.getProblemList(fhz);
                re.setData(list);
            }
        }
        return re;
    }


    @PostMapping("problemOperate")
    public ResultVo<Problem> problemOperate(@RequestParam(value = "active") int active, Problem problem,
                                            ProblemSelect select,ProblemGap gap,ProblemProg prog, HttpSession session) throws ClassNotFoundException {
        ResultVo re = new ResultVo();
        select.setProblem_id(problem.getProblem_id());
        gap.setProblem_id(problem.getProblem_id());
        prog.setProblem_id(problem.getProblem_id());
        User userLogin = (User)session.getAttribute("login");
        if(userLogin!=null){
            Role role = (Role)session.getAttribute("role");
            if(role.getIs_ope_problem() == 0){
                re.setStatusCode(0);
                re.setMessage("没有权限");
            }else {
                re.setStatusCode(1);
                if(active == 0){
                    re.setMessage("修改成功");
                    if(problem.getProblem_type().equals("problem_select")){
                           service.updateProblem(problem,select);
                           re.setData(service.getProblem(problem,1));
                    }else if(problem.getProblem_type().equals("problem_gap")){
                            service.updateProblem(problem,gap);
                            re.setData(service.getProblem(problem,2));
                    }else{
                        service.updateProblem(problem,prog);
                        re.setData(service.getProblem(problem,3));
                    }
                }else if(active == 1){

                    re.setMessage("添加成功");
                    if(problem.getProblem_type().equals("problem_select")){
                        service.addProblem(problem,select);
                        re.setData(service.getProblem(problem,1));
                    }else if(problem.getProblem_type().equals("problem_gap")){
                        service.addProblem(problem,gap);
                        re.setData(service.getProblem(problem,2));
                    }else{
                        service.addProblem(problem,prog);
                        Problem p2 = service.getProblem(problem,3);
                        ProblemProg pr = (ProblemProg) p2.getType();
                        String separator = File.separator;
                        String directory = "/home"+separator+"judge"+separator+"data"+separator+problem.getProblem_id();
                        String fileName1 = "test.in";
                        String fileName2 = "test.out";
                        try {
                            Runtime.getRuntime().exec("chmod 777 -R " + directory);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        File f1 = new File(directory,fileName1);
                        File f2 = new File(directory,fileName2);

                        f1.getParentFile().mkdirs();
                        try {
                            f1.createNewFile();
                            f2.createNewFile();

                            File file1 = new File("/home/judge/data/"+problem.getProblem_id()+"/test.in");
                            File file2 = new File("/home/judge/data/"+problem.getProblem_id()+"/test.out");
                            if (!file1.exists()) {
                                file1.createNewFile();
                            }
                            if (!file2.exists()) {
                                file2.createNewFile();
                            }
                            FileWriter fw1 = new FileWriter(directory+"test.in");
                            FileWriter fw2 = new FileWriter(directory+"test.out");

                            fw1.write(pr.getSample_input());
                            fw1.flush();
                            fw2.write(pr.getSample_output());
                            fw2.flush();
                            re.setMessage("添加成功");
                            re.setData(p2);
                        } catch (IOException e) {
                            re.setMessage("添加失败");
                            e.printStackTrace();
                        }
                    }

                }else if(active == -1){
                    re.setMessage("删除成功");
                    Problem problem1 = service.getProblem(problem);
                    if(problem1.getProblem_type().equals("problem_select")){
                        Problem p1 = service.getProblem(problem,1);
                        re.setData(p1);
                    }else if(problem1.getProblem_type().equals("problem_gap")){
                        Problem p1 = service.getProblem(problem,2);
                        re.setData(p1);
                    }else{
                        Problem p1 = service.getProblem(problem,3);
                        re.setData(p1);
                    }
                }
            }
        }
        return re;
    }

}
